//
//  ZYXSearchResultVC.m
//  RuiBeiKang
//
//  Created by iOS开发1 on 2019/1/7.
//  Copyright © 2019年 JW. All rights reserved.
//

#import "ZYXSearchResultVC.h"

#import "ZYXSearchResultCell.h"
#import "ZYXSearchTopView.h"
#import "ZYXClassificationModel.h"
#import "ZYXVideoPlayVC.h"
@interface ZYXSearchResultVC ()<UITextFieldDelegate>
@property (strong,nonatomic) NSMutableArray *dataArr;
/// 假导航
@property (nonatomic,strong) ZYXSearchTopView *topSearchView;
///页数
@property (strong,nonatomic) NSString *page;
@end

@implementation ZYXSearchResultVC
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}



#pragma mark MainUI
-(void)setupViews{
    [self setupTopNavView];
    self.view.backgroundColor = [UIColor whiteColor];
    self.tableView.frame = CGRectMake(0, NavigationBar_Height, SCREEN_WIDTH, SCREEN_HEIGHT-NavigationBar_Height);
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"ZYXSearchResultCell" bundle:nil] forCellReuseIdentifier:@"ZYXSearchResultCell"];
    [self.view addSubview:self.tableView];
    self.page = @"1";
    [self loadRequestPage:self.page];
    [self setupRefresh];
    
}
/// 假导航栏
- (void)setupTopNavView {
    ZYXSearchTopView *searchView = [[NSBundle mainBundle] loadNibNamed:@"ZYXSearchTopView" owner:nil options:nil].lastObject;
    _topSearchView = searchView;
    
    [self.view addSubview:searchView];
    //    searchView.frame = CGRectMake(0, 0, SCREEN_WIDTH, NavigationBar_Height);
    [searchView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(20);
        make.left.offset(0);
        make.right.offset(0);
        make.height.offset(NavigationBar_Height-StatusBar_Height);
    }];
    searchView.searchImg = IMG(@"sousuo");
    searchView.searchField.placeholder = self.searchStr;
    searchView.searchField.delegate = self;
    searchView.cancelBtn.hidden = YES;
    [searchView.cancelBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [searchView.backBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
}
// MARK: -- textfield代理
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [self.view endEditing:YES];
   //输入新的字符去搜索
    NSString *fullStr = [Functions trimAndLowercase:textField.text];
    if (fullStr.length >0) {
        self.searchStr = fullStr;
        self.page = @"1";
         [self.dataArr removeAllObjects];
        [self loadRequestPage:@"1"];
    }else{
         [JKToast showWithText:@"您输入的为空字符串"];
    }
    
    return YES;
}
#pragma mark 加载数据
- (void)setupRefresh {
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page = @"1";
        [self.dataArr removeAllObjects];
        [self loadRequestPage:self.page];
    }];
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        self.page = SF(@"%ld",[self.page integerValue] + 1);
        [self loadRequestPage:self.page];
    }];
    /// 设置contentInset 补缺
    self.tableView.mj_footer.ignoredScrollViewContentInsetBottom = HomeIndicator_Height;
}
- (void)endRefresh {
    [self.tableView.mj_footer endRefreshing];
    [self.tableView.mj_header endRefreshing];
}
#pragma mark 加载数据
- (void)loadRequestPage:(NSString *)page {

    [NetWork POSTWithUrlString:kInterfaceAllUrl(PATH_IndexSearchSearch) parmas:@{@"search_name":self.searchStr,@"page":self.page} successBloock:^(NSDictionary *responseObject) {
        NSLog(@"%@",responseObject);
        if ([responseObject[@"code"] isEqualToNumber:@200]) {
            if (self.page <= responseObject[@"data"][@"last_page"]) {
                
                NSArray *modelArr = [ZYXClassificationModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"data"]];
                [self.dataArr addObjectsFromArray:modelArr];
                [self.tableView reloadData];
                
            }else{
                [JKToast showWithText:@"暂无加载更多内容"];
            }
            
        }else{
            [JKToast showWithText:responseObject[@"message"]];
        }
        [self endRefresh];
    } failureBlock:^(NSError *error) {
        [self endRefresh];
    }];
}
#pragma mark tableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ZYXSearchResultCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZYXSearchResultCell"];
    cell.model = self.dataArr[indexPath.row];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return HEIGHT(85);
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"点击");
    ZYXClassificationModel *model = self.dataArr[indexPath.row];
    ZYXVideoPlayVC *vc = [[ZYXVideoPlayVC alloc]init];
    vc.zid = model.uid;
    vc.title = model.title;
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark 懒加载
-(NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}


@end
